package org.example.page;

import lombok.extern.log4j.Log4j2;
import org.example.param.Parameter;
import org.example.thread.FrameMoveThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@Log4j2
public class FrameMovePanel extends JPanel implements ActionListener {
    private JPanel cardPanel;
    private CardLayout cardLayout;

    private JFrame jFrame;
    private FrameMoveThread frameMoveThread;

    public FrameMovePanel(JPanel cardPanel, CardLayout cardLayout, JFrame jFrame) {
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;
        this.jFrame = jFrame;

        btnInit();
    }


    private void btnInit() {
        JButton returnBtn = new JButton(Parameter.RETURN_BTN);
        JButton startBtn = new JButton(Parameter.START_BTN);
        JButton endBtn = new JButton(Parameter.END_BTN);

        returnBtn.addActionListener(this);
        startBtn.addActionListener(this);
        endBtn.addActionListener(this);


        this.add(returnBtn);
        this.add(startBtn);
        this.add(endBtn);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton){
            JButton btn = (JButton) e.getSource();
            switch (btn.getText()){
                case Parameter.START_BTN ->
                        startFrameMoveThread();
//                     SwingUtilities.invokeLater(frameMoveThread);
                case Parameter.END_BTN ->
                        endFrameMoveThread();

                case Parameter.RETURN_BTN ->
                    cardLayout.show(cardPanel,"mainPanel");
            }

        }
    }

    private void endFrameMoveThread() {
        frameMoveThread.stopMove();

    }

    private void startFrameMoveThread() {
        frameMoveThread = new FrameMoveThread(jFrame);
        frameMoveThread.start();

    }
}
